package com.njupt.sellsec.controller;

import com.njupt.sellsec.access.AccessLimit;
import com.njupt.sellsec.handler.UserContext;
import com.njupt.sellsec.vo.GoodDetailVO;
import com.njupt.sellsec.domain.SellUser;
import com.njupt.sellsec.redis.RedisService;
import com.njupt.sellsec.result.Result;
import com.njupt.sellsec.service.GoodsService;
import com.njupt.sellsec.service.Impl.SellUserServiceImpl;
import com.njupt.sellsec.service.SellUserService;
import com.njupt.sellsec.vo.GoodsFormVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 17:43 2018/7/28
 * @Modified By:
 **/
@Controller
@RequestMapping("/goods")
@Slf4j
public class SellGoodController {

    @Autowired
    private SellUserService sellUserService;

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisService redisService;

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 加对象缓存后：
     * 1000
     * QPS:672
     * @param model
     * @param response
     * @param request

     * @return
     */

    @GetMapping(value = "/to_list")
    public String toGoods(Model model,
                          HttpServletResponse response,
                          HttpServletRequest request
                          /*@CookieValue(value =SellUserServiceImpl.COOKIE_NAME_TOKEN,required = false)String cookieValue,
                          @RequestParam(value = SellUserServiceImpl.COOKIE_NAME_TOKEN,required = false)String paramToken*/){
        System.out.println("goods_list----------------------");
       /* if (StringUtils.isEmpty(cookieValue)&& StringUtils.isEmpty(paramToken)){
            return "login";
        }
        String token = StringUtils.isEmpty(paramToken)?cookieValue:paramToken;
        SellUser user = sellUserService.findUserByToken(response,token);*/
       SellUser user = UserContext.getUser();
        System.out.println("goods_list+++++++++++++++++++++"+user);
        model.addAttribute("user",user);

        List<GoodsFormVO> goodsFormVOList = goodsService.getGoodsVOListById();
        model.addAttribute("goodsList",goodsFormVOList);
        //todo 页面缓存未完成



        return "goods_list";
    }

    /**         1000
     *          QPS:590
     *
     * @param model
     * @param response

     * @param goodsId
     * @return
     */
    @GetMapping("/to_detail2/{id}")
    public String toGoodsDetail2(Model model,
                                HttpServletResponse response,
                               /* @CookieValue(value =SellUserServiceImpl.COOKIE_NAME_TOKEN,required = false)String cookieValue,
                                @RequestParam(value = SellUserServiceImpl.COOKIE_NAME_TOKEN,required = false)String paramToken,*/
                                @PathVariable("id")Long goodsId){
        /*if (StringUtils.isEmpty(cookieValue)&& StringUtils.isEmpty(paramToken)){
            return "login";
        }
        String token = StringUtils.isEmpty(paramToken)?cookieValue:paramToken;
        SellUser user = sellUserService.findUserByToken(response,token);*/
        SellUser user = UserContext.getUser();
        model.addAttribute("user",user);

        GoodsFormVO goodsFormVO = goodsService.getGoodInfoById(goodsId);

        long startAt = goodsFormVO.getStartDate().getTime();
        long endAt = goodsFormVO.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int secSellStatus = 0;
        int remainSecond = 0;
        if (startAt>now){//秒杀还未开始
            secSellStatus = 0;
            remainSecond = (int) ((startAt-now)/1000);
        }else if (now>endAt){//秒杀已经结束
            secSellStatus = 2;
        }else {
            secSellStatus = 1;
        }
        model.addAttribute("goods",goodsFormVO);
        model.addAttribute("secStatus",secSellStatus);
        model.addAttribute("remainSeconds",remainSecond);

        //todo 页面缓存待完成
        return "goods_detail";

    }

    /**
     * 页面静态化之后的QPS
     *  1000
     *  QPS：641
     *
     *
     *
     * * @param model
     * @param response

     * @param goodsId
     * @return
     * @throws IOException
     */
    @GetMapping("/detail/{id}")
    @ResponseBody
    public Result<GoodDetailVO> toGoodsDetail(Model model,
                                 HttpServletResponse response,
                                 /*@CookieValue(value =SellUserServiceImpl.COOKIE_NAME_TOKEN,required = false)String cookieValue,
                                 @RequestParam(value = SellUserServiceImpl.COOKIE_NAME_TOKEN,required = false)String paramToken,*/
                                 @PathVariable("id")Long goodsId) throws IOException {
        /*if (StringUtils.isEmpty(cookieValue)&& StringUtils.isEmpty(paramToken)){
            response.sendRedirect("/login/to_login");
            return null;
        }
        String token = StringUtils.isEmpty(paramToken)?cookieValue:paramToken;
        SellUser user = sellUserService.findUserByToken(response,token);*/
        SellUser user = UserContext.getUser();
        System.out.println("toGoodsDetail"+user);
        model.addAttribute("user",user);

        GoodsFormVO goodsFormVO = goodsService.getGoodInfoById(goodsId);

        long startAt = goodsFormVO.getStartDate().getTime();
        long endAt = goodsFormVO.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int secSellStatus = 0;
        int remainSecond = 0;
        if (startAt>now){//秒杀还未开始
            secSellStatus = 0;
            remainSecond = (int) ((startAt-now)/1000);
        }else if (now>endAt){//秒杀已经结束
            secSellStatus = 2;
        }else {
            secSellStatus = 1;
        }
        /*model.addAttribute("goods",goodsFormVO);
        model.addAttribute("secStatus",secSellStatus);
        model.addAttribute("remainSeconds",remainSecond);*/

        //页面静态化
        GoodDetailVO goodDetailVO = new GoodDetailVO();
        goodDetailVO.setGoods(goodsFormVO);
        goodDetailVO.setRemainSecond(remainSecond);
        goodDetailVO.setSecSellStatus(secSellStatus);
        goodDetailVO.setUser(user);


        return Result.success(goodDetailVO);

    }
}
